package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zzy
 * @date 2022/7/26 18:15
 */

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult inster(WmSensitive wmSensitive) {
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive sensitive = getOne(wrapper);
        if (sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"重复了");
        }

        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //查询列表
    @Override
    public ResponseResult find(SensitiveDto sensitiveDto) {
        if (sensitiveDto==null){
            return new PageResponseResult(0,0,0);
        }
        sensitiveDto.checkParam();
        Integer page = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();

        Page<WmSensitive> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());

        Page<WmSensitive> wmSensitivePage = wmSensitiveMapper.selectPage(pageInfo, wrapper);

        long total = wmSensitivePage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, (int) total);

        pageResponseResult.setData(wmSensitivePage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult update(WmSensitive wmSensitive) {

        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,wmSensitive.getId());
        Integer count = wmSensitiveMapper.selectCount(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"重复");
        }

        updateById(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmSensitive byId = getById(id);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(byId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
